package com.gs.process.admin.config;

import com.gs.process.core.interceptor.ProcessConfigurer;
import com.gs.process.core.interceptor.ProcessContextManager;
import com.gs.process.core.token.TokenUtil;
import com.gs.process.core.user.ProcessUserService;
import com.gs.process.core.user.SysUser;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @author: Shuai Guo
 * @description:
 * @date: 2021/9/28
 * @version: 1.0
 */
@Configuration
public class MyProcessConfig implements ProcessConfigurer {

    @Resource
    private ProcessUserService processUserService;

    @Override
    public void configurer(ProcessContextManager manager) {
        manager.addInterceptor(httpServletRequest -> {
            String token = httpServletRequest.getHeader("token");
            if (ObjectUtils.isEmpty(token)) {
                token = httpServletRequest.getParameter("token");
            }
            SysUser sysUser = TokenUtil.parseToUser(token);
            processUserService.set(sysUser);
        });
    }
}
